//
//  IWExpenseItem.m
//  SqliteTrial
//
//  Created by Aries on 11-7-29.
//  Copyright 2011年 windvoice. All rights reserved.
//

#import "IWExpenseItem.h"
#import "ISqlite.h"


@implementation IWExpenseItem

@synthesize ID,Title,Amount,Remark,IsExpense,ExpenseTime,CreateTime;

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
        database = [[ISqlite alloc] initWithDatabaseFileName:@"wallet.db"];
    }
    
    return self;
}

- (id) initWithID:(int) itemID
{
    self = [super init];
    if (self) {        
        database = [[ISqlite alloc] initWithDatabaseFileName:@"wallet.db"];
        
        NSArray *result = [database executeTable:[NSString stringWithFormat: @"select * from expense where id=%d",itemID]];
        
        if(result!=nil && [result count]==1)
        {
            NSDictionary *row = [result objectAtIndex:0];
            
            NSDateFormatter *dateFormatter = [NSDateFormatter new];
            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            
            self.ID = itemID;
            self.Title = [[row valueForKey:@"title"] description];
            self.Amount = [[[row valueForKey:@"amount"] description] floatValue];
            self.Remark = [[row valueForKey:@"remark"] description];
            self.IsExpense = [[[row valueForKey:@"isexpense"] description] boolValue];
            self.ExpenseTime = [dateFormatter dateFromString:[[row valueForKey:@"expensetime"] description]];
            self.CreateTime = [dateFormatter dateFromString:[[row valueForKey:@"createtime"] description]];
            
            [dateFormatter release];
        }
    }
    
    return self;
}

- (int) checkSchema
{
    NSString *command = @"select * from sqlite_master where type='table'";
    if([[database executeTable:command] count]<=0)
    {
        command = @"create table expense (id integer PRIMARY KEY,title text,amount float,remark text,isexpense int,expensetime datetime,createtime datetime)";    
        [database executeNonQuery:command];
    }
    
    return 1;
}

- (int) update
{
    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSString *command = [NSString stringWithFormat:@"update expense set title='%@',amount=%f,remark='%@',isexpense=%d,expensetime='%@',createtime='%@' where id=%d",self.Title,self.Amount,self.Remark,self.IsExpense?1:0,[dateFormatter stringFromDate:self.ExpenseTime],[dateFormatter stringFromDate:self.CreateTime ],self.ID];
    
    [dateFormatter release];
    
    return [database executeNonQuery:command];
}

- (int) save
{    
    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSString *command = [NSString stringWithFormat:@"insert into expense(title,amount,remark,isexpense,expensetime,createtime) values('%@',%f,'%@',%d,'%@','%@')",self.Title,self.Amount,self.Remark,self.IsExpense?1:0,[dateFormatter stringFromDate:self.ExpenseTime],[dateFormatter stringFromDate:self.CreateTime ]];
    
    //NSLog(command);
    [dateFormatter release];
    
    return [database executeNonQuery:command];
}

- (int) remove
{
    NSString *command = [NSString stringWithFormat: @"delete from expense where id=%d",self.ID ];
    return  [database executeNonQuery:command];
}

+ (int) removeWithID:(int) itemID
{
    ISqlite *db = [[ISqlite alloc] initWithDatabaseFileName:@"wallet.db"];
    NSString *command = [NSString stringWithFormat: @"delete from expense where id=%d",itemID ];
    int result = [db executeNonQuery:command];
    [db release];
    return result;
}

+ (NSArray*) getList
{
    ISqlite *db = [[ISqlite alloc] initWithDatabaseFileName:@"wallet.db"];
    
    NSString *command = @"select * from expense order by id desc";
    
    NSArray *table = [db executeTable:command];
    
    [db release];
    
    NSMutableArray *array = [NSMutableArray new];
    [array autorelease];
    
    for(NSDictionary *row in table)
    {
        NSDateFormatter *dateFormatter = [NSDateFormatter new];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        
        IWExpenseItem *item = [IWExpenseItem new];
        
        item.ID = [[[row valueForKey:@"id"] description] intValue];
        item.Title = [[row valueForKey:@"title"] description];
        item.Amount = [[[row valueForKey:@"amount"] description] floatValue];
        item.Remark = [[row valueForKey:@"remark"] description];
        item.IsExpense = [[[row valueForKey:@"isexpense"] description] boolValue];
        item.ExpenseTime = [dateFormatter dateFromString:[[row valueForKey:@"expensetime"] description]];
        item.CreateTime = [dateFormatter dateFromString:[[row valueForKey:@"createtime"] description]];
        
        [dateFormatter release];
        
        [array addObject:item];
        [item release];
    }
    
    return array;
}


- (NSString *)description
{
    NSString *des = [[NSString alloc] init];
    [des autorelease];
    des = [NSString stringWithFormat:@"ID:%d;Title:%@;Amount:%f;IsExpense:%d;Remark:%@;CreateTime:%@",self.ID,self.Title,self.Amount,self.IsExpense==YES?1:0,self.Remark,[self.CreateTime description]];
    
    return des;
}


- (NSString*) des
{
    return [NSString stringWithFormat:@"%d",self.ID];
}

- (void)dealloc
{
    [self.Title release];
    [self.Remark release];
    [self.ExpenseTime release];
    [self.CreateTime release];
    [super dealloc];
}

@end
